package com.example.sociamedia.interaction.controller;

import com.example.sociamedia.interaction.dto.FollowRequest;
import com.example.sociamedia.interaction.dto.FollowResponse;
import com.example.sociamedia.interaction.entity.Follow;
import com.example.sociamedia.interaction.mapper.FollowMapper;
import com.example.sociamedia.user.entity.User;
import com.example.sociamedia.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class FollowService implements FollowApi {
    private final FollowMapper followMapper;
    private final UserMapper userMapper;

    @Autowired
    public FollowService(FollowMapper followMapper, UserMapper userMapper) {
        this.followMapper = followMapper;
        this.userMapper = userMapper;
    }

    @Override
    public FollowResponse followUser(String sessionToken, FollowRequest request) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) {
            FollowResponse resp = new FollowResponse();
            resp.setSuccess(false);
            resp.setMessage("无效的用户token");
            return resp;
        }
        if (user.getUserid().equals(request.getFollowingId())) {
            FollowResponse resp = new FollowResponse();
            resp.setSuccess(false);
            resp.setMessage("不能关注自己");
            return resp;
        }
        Follow exist = followMapper.selectFollow(user.getUserid(), request.getFollowingId());
        if (exist != null) {
            FollowResponse resp = new FollowResponse();
            resp.setSuccess(false);
            resp.setMessage("已关注该用户");
            return resp;
        }
        Follow follow = new Follow();
        follow.setFollowerId(user.getUserid());
        follow.setFollowingId(request.getFollowingId());
        followMapper.insertFollow(follow);
        FollowResponse resp = new FollowResponse();
        resp.setSuccess(true);
        resp.setMessage("关注成功");
        return resp;
    }

    @Override
    public FollowResponse unfollowUser(String sessionToken, FollowRequest request) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) {
            FollowResponse resp = new FollowResponse();
            resp.setSuccess(false);
            resp.setMessage("无效的用户token");
            return resp;
        }
        int count = followMapper.deleteFollow(user.getUserid(), request.getFollowingId());
        FollowResponse resp = new FollowResponse();
        if (count > 0) {
            resp.setSuccess(true);
            resp.setMessage("取关成功");
        } else {
            resp.setSuccess(false);
            resp.setMessage("未关注该用户");
        }
        return resp;
    }

    @Override
    public List<Follow> getFollowings(Long userId) {//我关注了谁
        return followMapper.selectFollowings(userId);
    }

    @Override
    public List<Follow> getFollowers(Long userId) {//谁关注了我
        return followMapper.selectFollowers(userId);
    }

    public com.example.sociamedia.user.entity.User getUserByToken(String sessionToken) {
        return userMapper.selectUserByToken(sessionToken);
    }

    public boolean isFollowing(Long myId, Long targetUserId) {
        return followMapper.selectFollow(myId, targetUserId) != null;
    }
} 